package com.fg.cloud.framework.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.construction.Tree;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.system.RoleEntity;
import com.fg.cloud.manager.service.system.MenuService;
import com.fg.cloud.manager.service.system.RoleService;

/**
 * Description:系统角色controller
 * 
 * @author shaoqiping
 * @date 2017年8月8日上午9:00:49
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;

	@Autowired
	private MenuService role_menuService;

	/**
	 * Description:角色列表分页查询
	 * 
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param filterText
	 * @author shaoqiping
	 * @date 2017年8月9日下午2:43:46
	 */
	@RequestMapping(value = "/findList")
	@ResponseBody
	public void select(HttpServletRequest request, HttpServletResponse response, Integer page, Integer rows,
			String sort, String order, String filterText) {
		try {
			if (page == null || page <= 0) {
				page = 1;
			}
			if (rows == null || rows <= 0) {
				rows = 10;
			}
			PageUtils<RoleEntity> pageinfo = new PageUtils<RoleEntity>(page, rows, sort, order);
			Map<String, Object> condition = pageinfo.getCondition();
			condition.put("filterText", filterText);
			condition.put("facId", getCurrentFactoryUser().getFacId());
			pageinfo.setCondition(condition);
			roleService.findList(pageinfo);
			doResult(response, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	  * Description:查询指定角色信息
	  * @param request
	  * @param response
	  * @param id
	  * @author shaoqiping
	  * @date 2017年8月16日下午1:50:51
	*/
	@RequestMapping(value = "/find")
	@ResponseBody
	public void selectRole(HttpServletRequest request, HttpServletResponse response, Long id) {
		try {
			if (LongUtils.isEmpty(id) || id == null) {
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.CHOOSE_INFO);
				return;
			}
			RoleEntity roleEntity = roleService.find(id);
			if (roleEntity == null || !roleEntity.getFacId().equals(getCurrentFactoryUser().getFacId())) {
				doResult(response, false, StatusCode.FINDERROR, null, ResultType.FIND_NULL);
				return;
			}
			doResult(response, true, StatusCode.FINDSUCCESS, roleEntity, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:新增角色的信息
	 * 
	 * @param request
	 * @param response
	 * @param roleEntity
	 *            实体类对象
	 * @param iconFile
	 * @author shaoqiping
	 * @date 2017年8月8日上午9:24:37
	 */
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	@ResponseBody
	public void insert(HttpServletRequest request, HttpServletResponse response, RoleEntity roleEntity) {
		try {
			if (roleEntity == null || StringUtils.isEmpty(roleEntity.getRoleName())) {
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
				return;
			}
			roleEntity.setFacId(getCurrentFactoryUser().getFacId());
			if (roleService.findByRoleName(roleEntity) != null) {
				doResult(response, false, StatusCode.SAVEERROR, null, "角色已存在");
				return;
			}
			setCreateInfo(roleEntity, request);
			setUpdateInfo(roleEntity, request);
			boolean temp = roleService.insertSelective(roleEntity);
			if (temp) {
				doResult(response, true, StatusCode.SAVESUCCESS, null, ResultType.ADD_SUCCESS);
			} else {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.ADD_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:更改某个角色信息
	 * 
	 * @param request
	 * @param response
	 * @param roleEntity
	 * @author shaoqiping
	 * @date 2017年8月8日上午11:44:45
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public void update(HttpServletRequest request, HttpServletResponse response, RoleEntity roleEntity) {
		try {
			// 条件判断
			if (roleEntity == null || roleEntity.getId() == null || roleEntity.getId() < 0) {
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.CHOOSE_INFO);
				return;
			}
			roleEntity.setFacId(getCurrentFactoryUser().getFacId());
			RoleEntity flag=roleService.findByRoleName(roleEntity);
			if (flag != null&&!flag.getId().equals(roleEntity.getId())) {
				doResult(response, false, StatusCode.SAVEERROR, null, "角色已存在");
				return;
			}
			setUpdateInfo(roleEntity, request);
			boolean temp = roleService.updateByPrimaryKeySelective(roleEntity);
			if (temp) {
				doResult(response, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
			} else {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:批量删除角色信息
	 * 
	 * @param request
	 * @param response
	 * @param ids
	 * @author shaoqiping
	 * @date 2017年8月8日上午11:42:36
	 */
	@RequestMapping(value = "/deletes")
	@ResponseBody
	public void delete(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "ids", required = true) String ids) {
		try {
			if (ids == null || StringUtils.isEmpty(ids)) {
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.CHOOSE_INFO);
				return;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ids", ids);
			map.put("facId", getCurrentFactoryUser().getFacId());
			boolean temp = roleService.deletes(map);
			if (temp) {
				doResult(response, true, StatusCode.REMOVESUCCESS, null, ResultType.REMOVE_SUCCESS);
			} else {
				doResult(response, false, StatusCode.REMOVEERROR, null, ResultType.REMOVE_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:角色分配菜单操作 -- 查询
	 * 
	 * @param request
	 * @param response
	 * @param roleEntity
	 * @param classify
	 * @author shaoqiping
	 * @date 2017年8月10日上午9:44:41
	 */
	@RequestMapping(value = "/findMenuByRoleId")
	@ResponseBody
	public void findMenuByRole(HttpServletRequest request, HttpServletResponse response, Long id, Integer classify) {
		try {
			if (id == null || LongUtils.isEmpty(id) || classify == null || classify < 0) {
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.CHOOSE_INFO);
				return;
			}
			if(roleService.findToCheck(id)==null){
				doResult(response, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
				return;
			}
			if (!roleService.findToCheck(id).getFacId().equals(getCurrentFactoryUser().getFacId())
					&& !roleService.findToCheck(id).getFacId().equals(0)) {
				doResult(response, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
				return;
			}
			List<Tree> trees = role_menuService.findByRoleId(id, classify);
			Map<String,List<Tree>> map = new HashMap<String,List<Tree>>();
			map.put("a", trees);
			if(classify==1){
				map.put("b", role_menuService.findByRoleId(id, 3));
			}else{
				map.put("b", null);
			}
			doResult(response, true, StatusCode.FINDSUCCESS, map, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:角色分配菜单操作 -- 修改
	 * 
	 * @param request
	 * @param response
	 * @param roleEntity
	 * @param menuIds
	 * @author shaoqiping
	 * @date 2017年8月10日上午9:44:54
	 */
	@RequestMapping(value = "/updateMenuRole", method = RequestMethod.POST)
	@ResponseBody
	public void updateMenuRole(HttpServletRequest request, HttpServletResponse response, Long id, String menuIds) {
		try {
			if (id == null || LongUtils.isEmpty(id) || menuIds == null) {
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.CHOOSE_INFO);
				return;
			}
			if(roleService.findToCheck(id)==null){
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
				return;
			}
			if (!roleService.findToCheck(id).getFacId().equals(getCurrentFactoryUser().getFacId())
					&& !roleService.findToCheck(id).getFacId().equals(0)) {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
				return;
			}
			boolean flag = roleService.updateMenuRoles(id, menuIds);
			doResult(response, flag, flag ? StatusCode.SAVESUCCESS : StatusCode.SAVEERROR, null,
					flag ? ResultType.SAVE_SUCCESS : ResultType.SAVE_FAULT);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

}
